/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.planning.tactical.wizard.common;

import java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import net.miginfocom.swing.MigLayout;
import nl.cloudfarming.client.planning.tactical.TacticalPlanningModule;
import nl.cloudfarming.client.planning.tactical.wizard.spray.PlanningWizardAction;
import org.jdesktop.swingx.JXPanel;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.util.NbBundle;


/**
 * Action which wil provide a panel on which the prefered action can be selected.
 * @author Merijn Zengers
 */
public class PlanActionWizardAction extends AbstractAction {
    
    private final String DISPLAY_NAME = NbBundle.getMessage(TacticalPlanningModule.class, "action.wizard.choose.actiontype.name");
    private final String ACTION_OPTION_ONE = NbBundle.getMessage(TacticalPlanningModule.class,"action.wizard.choose.actiontype.combobox.option1");
    private final String CHOICE_LABEL = NbBundle.getMessage(TacticalPlanningModule.class, "action.wizard.choose.actiontype.label.choose.action");
    private final String OK_BUTTON_LABEL = NbBundle.getMessage(TacticalPlanningModule.class, "action.wizard.choose.actiontype.button.ok");
    private final String CANCEL_BUTTON_LABEL = NbBundle.getMessage(TacticalPlanningModule.class, "action.wizard.choose.actiontype.button.cancel");
    
    private Dialog dlg;
    private JComboBox actionComboBox;
    private DefaultComboBoxModel actionModel;
    private JXPanel panel;
    
    public PlanActionWizardAction() {
        putValue(NAME, DISPLAY_NAME);
        panel = new JXPanel(new MigLayout("wrap 2"));
        actionModel = new DefaultComboBoxModel(new Object[]{ACTION_OPTION_ONE});
        actionComboBox = new JComboBox(actionModel);
        panel.setName(DISPLAY_NAME);
        panel.add(new JLabel(CHOICE_LABEL));
        panel.add(actionComboBox); 
    }
    
    @Override
    public void actionPerformed(ActionEvent e) { 
        DialogDescriptor dd = new DialogDescriptor(panel, DISPLAY_NAME);
        dd.setOptions(new Object[]{getOkButton(), getCancelButton()});
        dlg = DialogDisplayer.getDefault().createDialog(dd);
        dlg.setVisible(true);
    }
    
    /**
     * creates the dialog ok button
     *
     * @return
     */
    private JButton getOkButton() {
        JButton jButtonOK = new JButton();
        jButtonOK.setText(OK_BUTTON_LABEL);
        jButtonOK.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                dlg.setVisible(false);
                new PlanningWizardAction().actionPerformed(new ActionEvent(this, 0, DEFAULT));
            }
        });
        return jButtonOK;
    }

    /**
     * creates the dialog cancel button
     * @return
     */
    private JButton getCancelButton() {
        JButton jButtonCancel = new JButton();
        jButtonCancel.setText(CANCEL_BUTTON_LABEL);
        jButtonCancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                dlg.setVisible(false);
            }
        });
        return jButtonCancel;
    }

    JXPanel getPanel() {
        return panel;
    }
    
}
